import { _decorator, Component, EventTouch, find, instantiate, log, Node } from 'cc';
import { tongyongTS } from '../tongyongTS/tongyongTS';
const { ccclass, property } = _decorator;

@ccclass('mlgmBJQTS')
export class mlgmBJQTS extends Component {
    ty: tongyongTS
    d打底: Node

    c初始x = -297.5
    c初始y = -160
    j间隔距离x = 35
    j间隔距离y = 50

    z总行数 = 11
    z总列数 = 18

    b表格sz = []
    protected onLoad(): void {
        this.ty = find('通用管理').getComponent(tongyongTS)
        this.d打底 = this.ty.find查找('Canvas/打底')
    }

    start() {
        for (let hh = 0; hh < this.z总行数; hh++) {
            for (let lh = 0; lh < this.z总列数; lh++) {
                this.b表格sz.push(-1)
                let n = instantiate(this.d打底)
                n.setParent(find('Canvas/打底组'))
                this.ty.s设置位置(n, lh, hh, this.c初始x, this.c初始y, this.j间隔距离x, this.j间隔距离y)
                n.on(Node.EventType.TOUCH_START, this.点击执行1, this)
            }
        }
    }

    点击执行1(e: EventTouch) {
        let n = e.target
        let v = n.position
        let lh = (v.x - this.c初始x) / this.j间隔距离x
        let hh = (v.y - this.c初始y) / this.j间隔距离y

        let tj = true
        if (hh + 1 < this.z总行数) {
            if (this.b表格sz[this.zdh(hh + 1, lh)] != -1) {
                tj = false
            }
        }
        if (hh - 1 >= 0) {
            if (this.b表格sz[this.zdh(hh - 1, lh)] != -1) {
                tj = false
            }
        }
        if (lh - 1 >= 0) {
            console.log('asd')
            if (this.b表格sz[this.zdh(hh, lh - 1)] != -1) {
                tj = false
            }
        }
        if (lh + 1 < this.z总列数) {
            if (this.b表格sz[this.zdh(hh, lh + 1)] != -1) {
                tj = false
            }
        }
        if (tj) {
            this.b表格sz[this.zdh(hh, lh)] = 0
            let n = instantiate(find('Canvas/牌1'))
            n.setParent(find('Canvas/牌组'))
            this.ty.s设置位置(n, lh, hh, this.c初始x, this.c初始y, this.j间隔距离x, this.j间隔距离y)
            n.on(Node.EventType.TOUCH_START, this.d点击删除, this)
        }

    }


    d点击删除(e: EventTouch) {
        let n:Node = e.target
        let v = n.position
        let lh = (v.x - this.c初始x) / this.j间隔距离x
        let hh = (v.y - this.c初始y) / this.j间隔距离y

        this.b表格sz[this.zdh(hh, lh)] = -1
        n.destroy()
    }

    zdh(hh, lh) {
        let num = lh + hh * this.z总列数
        return num
    }   

    d打印(){
        let s=this.b表格sz
        console.log(JSON.stringify(s))
    }
}


